using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;
using DM.Entity;
using System.IO;

namespace DM.Web.Admin.QuanLyBaoCaoTaiChinh
{
    public partial class BaoCaoTaiChinhNam : System.Web.UI.Page
    {
        readonly TepDAO _tepService = new TepDAO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindingGridViewBaoCao();
            }
        }

        // Sử lý phần Upload

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (fuBaoCao.HasFile)
            {
                string fileName = Common.UploadFile(fuBaoCao, Request.PhysicalApplicationPath +"Files/");
                if (!string.IsNullOrEmpty(txtNam.Text)||!string.IsNullOrEmpty(fileName))
                {
                    Tep tep = new Tep();
                    tep.TenBaoCao = txtTenBC.Text;
                    tep.TenFile = fileName;
                    tep.IsTN = dlLoaiBaoCao.SelectedValue == "1" ? true : false;
                    tep.Nam = Common.ParseInt32(txtNam.Text);
                    tep.Quy = dlQuy.Visible ? Common.ParseInt32(dlQuy.SelectedValue) : 0;
                    _tepService.Insert(tep);

                    Common.Show("Upload file thành công", Request.UrlReferrer.ToString());
                }
                else
                {
                    Common.Show("Chưa nhập đủ số liệu yêu cầu!",Request.UrlReferrer.ToString());
                }
            }
            else
            {
                Common.Show("Bạn chưa chọn đường dẫn cho file!", Request.UrlReferrer.ToString());
            }
        }


        // Sử lý phần View báo cáo



        /// <summary>
        /// Gắn kết dữ liệu tương ứng vào GridView Bao Cao
        /// </summary>
        private void BindingGridViewBaoCao()
        {
            List<Tep>  listTep = new List<Tep>();
            listTep = dlLoaiBaoCao1.SelectedValue=="1" ? _tepService.GetTepBCTN() : _tepService.GetTepBCTC();
            gviewBaoCao.DataSource = listTep;
            gviewBaoCao.DataBind();
        }


        protected void gviewBaoCao_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gviewBaoCao.PageIndex = e.NewPageIndex;
            gviewBaoCao.DataBind();
        }

        protected void dlLoaiBaoCao1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingGridViewBaoCao();
        }

        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            GridViewRow currentRow = (GridViewRow)button.Parent.Parent;
            gviewBaoCao.EditIndex = currentRow.RowIndex;
            BindingGridViewBaoCao();
        }

        protected void ButtonXoa_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            GridViewRow currentRow = (GridViewRow)button.Parent.Parent;
            int idTep = Common.ParseInt32(gviewBaoCao.DataKeys[currentRow.RowIndex].Value.ToString());
            Tep tep = _tepService.Get(idTep);
            //Xoa Tep Trong CSDL
            _tepService.Delete(idTep); 
            //Xoa file trong thu muc File
            try
            {
                FileInfo file = new FileInfo(Common.PhysicalApplicationPath() + tep.TenFile);
                if (file.Exists) file.Delete(); 
            }
            catch(Exception ex)
            {
                Common.Show(ex.ToString(),Request.UrlReferrer.ToString());
            }

            //Đưa ra thông báo
            Common.Show("Đã xóa!", Request.UrlReferrer.ToString());
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            GridViewRow currentRow = (GridViewRow)button.Parent.Parent;
            //Lay ra 3 control trong grid
            DropDownList dlEditLoai = (DropDownList) currentRow.FindControl("dlEditLoai");
            DropDownList dlEditQuy = (DropDownList)currentRow.FindControl("dlEditQuy");
            TextBox txtEditNam = (TextBox) currentRow.FindControl("txtEditNam");
            TextBox txtEditTenBc = (TextBox) currentRow.FindControl("txtEditTenBC");

            //Lay ra id cua tep trong CSDL
            
            int idTep = Common.ParseInt32(gviewBaoCao.DataKeys[currentRow.RowIndex].Value.ToString());
            Tep tep = _tepService.Get(idTep);

            // Up date thong tin vao csdl
            tep.Ma = idTep;
            tep.TenBaoCao = txtEditTenBc.Text;
            tep.IsTN = (dlEditLoai.SelectedValue == "0") ? true : false;
            tep.Quy = (dlEditQuy.Enabled) ? (Common.ParseInt32(dlEditQuy.SelectedValue)):0;
            tep.Nam = Common.ParseInt32(txtEditNam.Text);

            _tepService.Update(tep);
            //Thong bao ket qua

            Common.Show("Đã cập nhật thành công",Request.UrlReferrer.ToString());
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            gviewBaoCao.EditIndex = -1;
            BindingGridViewBaoCao();
        }
    }
}
